//
//  timer.m
//  iCodeMacRun
//
//  Created by Andrew Barry on 3/04/09.
//  Copyright 2009 Barry Computer Services. All rights reserved.
//

#import "timer.h"
#import "ActionBridge.h"

jsvalue timerConstructor(jscontext *cx, jsframe *outer, jsvalue obj, jsvalue *argv, int argc) {
	if (argc <= 3) {
		jsvalue form = argv[0];
		jsvalue defn = argv[2];
		ActionBridge *bridge = [[ActionBridge alloc] initWithObject:obj withEvent:@"Action" withContext:cx withTarget:form];
		jsset(cx, obj, 0, "_bridge", 1 | (int) bridge);
		jscopy(cx, obj, defn, 0, "Action");
		jscopy(cx, obj, defn, 0, "period");
		jscopy(cx, obj, defn, 0, "repeating");
	}
	return obj;
}

jsvalue timerStart(jscontext *cx, jsframe *outer, jsvalue obj, jsvalue *argv, int argc) {
	ActionBridge *bridge = (ActionBridge *) (jsget(cx, obj, 0, "_bridge") & ~1);
	NSTimer *timer = idFromObject(cx, obj);
	if (timer) {
		[timer invalidate];
		[timer release];
	}
	
	int period = jsgetint(cx, obj, 0, "period", 1000);
	jsbool repeats = jsgetbool(cx, obj, 0, "repeating", 0);
		
	timer = [[NSTimer scheduledTimerWithTimeInterval:(period / 1000.0) target:bridge selector:@selector(doIt:) userInfo:bridge repeats:repeats != 0] retain];
	
	idToObject(cx, obj, timer);
	
	return JSVAL_TRUE;
}

/*
JSObject *cocoaTimer;

JSBool timerConstructor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
	printf("image construct\n");
	return JS_TRUE;
}

void timerFinalizer(JSContext *cx, JSObject *obj) {
	printf("image finalize\n");
}
*/

/*
jsvalue timerStart(JSContext *cx, jsframe *outer, jsvalue obj, jsval *argv, int argc) {
	ActionBridge *bridge = idFromReservedSlot(obj, 1);
	NSTimer *timer = idFromReservedSlot(obj, 0);
	if (timer) {
		[timer invalidate];
		[timer release];
	}
	
	int32 period = 1000;
	int32 repeats = 0;
	jsval v;
	JS_GetProperty(cx, obj, "period", &v);
	JS_ValueToECMAInt32(cx, v, &period);

	JS_GetProperty(cx, obj, "repeating", &v);
	JS_ValueToECMAInt32(cx, v, &repeats);

	timer = [[NSTimer scheduledTimerWithTimeInterval:(period / 1000.0) target:bridge selector:@selector(doIt:) userInfo:bridge repeats:repeats != 0] retain];
	
	JS_SetReservedSlot(cx, obj, 0, INT_TO_JSVAL((int) timer));
	
	return JS_TRUE;
}

JSBool timerStop(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
	NSTimer *timer = idFromReservedSlot(obj, 0);
	[timer invalidate];
	[timer release];
	JS_SetReservedSlot(cx, obj, 0, INT_TO_JSVAL(0));
	return JS_TRUE;
}

JSClass timerClass = {"Timer", JSCLASS_HAS_RESERVED_SLOTS(2), 
JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,JS_PropertyStub, 
JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,timerFinalizer 
};

JSPropertySpec timerProps[] = {
{0, 0, 0, 0, 0}
};

JSFunctionSpec timerFunctions[] = {
{"start", timerStart, 0, 0, 0 },
{"stop", timerStop, 0, 0, 0 },
{0, 0, 0, 0, 0}
};

JSObject *timerObject;

void registerTimer(JSContext *cx, JSObject *global) {
	JS_AddRoot(cx, &timerObject);

	timerObject = JS_InitClass(cx, global, NULL, &timerClass,
									timerConstructor, 0,
									timerProps,
									timerFunctions, 
									NULL, NULL);
	
}

JSObject *createTimerForWindow(JSObject *target) {
	JSObject *obj = nil;
	
	JS_AddRoot(cx, &obj);

	obj = JS_NewObject(cx, &timerClass, timerObject, nil);
	ActionBridge *bridge = [[ActionBridge alloc] initWithObject:obj withEvent:@"Action" withContext:cx withTarget:target];
	JS_SetReservedSlot(cx, obj, 0, INT_TO_JSVAL(0));
	JS_SetReservedSlot(cx, obj, 1, INT_TO_JSVAL((int) bridge));

	JS_RemoveRoot(cx, &obj);

	return obj;
}
*/

void registerTimer(jscontext *cx) {
	jsvalue timerClass = bindFunction(cx, timerConstructor);
	jsset(cx, cx->slots[13], 0, "Timer", timerClass);
	jsaddfield(cx, timerClass, 0, "start", bindFunction(cx, timerStart));
}
